define(['jquery', 'bootstrap', 'backend', 'table', 'form'], function ($, undefined, Backend, Table, Form) {

    var Controller = {
        index: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: 'school_class_subject/index/ids/'+ Config.class_id.class_id + location.search,
                    add_url: 'school_class_subject/add?class_id=' + Config.class_id.class_id,
                    edit_url: 'school_class_subject/edit?class_id=' + Config.class_id.class_id,
                    del_url: 'school_class_subject/del',
                    multi_url: 'school_class_subject/multi',
                    table: 'school_class_subject',
                }
            });

            var table = $("#table");

            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                pk: 'id',
                sortName: 'id',

                columns: [
                    [
                        {checkbox: true},
                        {field: 'id', title: __('Id')},
                        {field: 'total_class_hour', title: __('Total_class_hour')},
                        {field: 'use_class_hour', title: __('use_class_hour')},
                        {
                            field: 'createtime',
                            title: __('Createtime'),
                            operate: 'RANGE',
                            addclass: 'datetimerange',
                            formatter: Table.api.formatter.datetime
                        },
                        {field: 'schoolsubject.subject_name', title: __('Schoolsubject.subject_name')},
                        {field: 'schoolsubject.cost', title: '单课时费（元）'},
                        {
                            field: 'operate',
                            title: __('Operate'),
                            table: table,
                            events: Table.api.events.operate,
                            formatter: Table.api.formatter.operate
                        },
                        {
                            field: 'buttons',
                            width: "120px",
                            title: '其他操作',
                            table: table,
                            operate:false,
                            events: Table.api.events.operate,
                            buttons: [
                                {
                                    name: 'detail',
                                    text: '排课',
                                    title: function (row) {
                                        return row.schoolsubject.subject_name;
                                    },
                                    classname: 'btn btn-xs btn-success btn-dialog',
                                    url: function(row){
                                        return 'school_class_subject_schedule/index?ids=' + row.id +'&class_id=' + Config.class_id.class_id
                                    },
                                    callback: function (data) {
                                        //Layer.alert("接收到回传数据：" + JSON.stringify(data), {title: "回传数据"});
                                    }
                                }
                            ],
                            formatter: Table.api.formatter.buttons
                        }
                    ]
                ]
            });

            // 为表格绑定事件
            Table.api.bindevent(table);
        },
        add: function () {
            Controller.api.bindevent();
        },
        edit: function () {
            Controller.api.bindevent();
        },
        api: {
            bindevent: function () {
                Form.api.bindevent($("form[role=form]"));
            }
        }
    };
    return Controller;
});